package com.mcj;

import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.IntBinaryOperator;
import java.util.function.IntConsumer;
import java.util.function.IntPredicate;

public class LambdaDemo01 {
	
	public static void main(String[] args) {
		
		new Thread(new Runnable() {
			public void run() {
				System.out.println("run()");
			}
		}).start();
		
		new Thread(()->{
			System.out.println("lambda");
		}).start();
		
		System.out.println(calculateNum(new IntBinaryOperator() {
			@Override
			public int applyAsInt(int left, int right) {
				return left + right;
			}
		}));
		
		System.out.println(calculateNum(Integer::sum));
		
		printNum(new IntPredicate() {
			@Override
			public boolean test(int value) {
				return value > 5;
			}
		});
		
		printNum((value)->{
			return value > 9;
		});
		
		System.out.println(typeConver(new Function<String, Integer>() {
			@Override
			public Integer apply(String s) {
				return Integer.valueOf(s);
			}
		}));
		
		System.out.println(typeConver(s -> Integer.valueOf(s)).toString());
		
		System.out.println(typeConver(new Function<String, String>() {
			@Override
			public String apply(String s) {
				return s + "_mcj";
			}
		}));
		
		foreachArr(new IntConsumer() {
			@Override
			public void accept(int value) {
				System.out.println(value);
			}
		});
		
		foreachArr(value -> {
			System.out.println(value);
		});
	}
	
	public static void foreachArr(IntConsumer consumer){
		int[] arr = {1,2,3,4,5,6,7,8,9,10};
		for (int i : arr) {
			consumer.accept(i);
		}
	}
	
	public static <R> R typeConver(Function<String,R> function){
		String str = "1235";
		return function.apply(str);
	}
	
	public static int calculateNum(IntBinaryOperator operator) {
		int a = 10;
		int b = 20;
		return operator.applyAsInt(a, b);
	}
	
	public static void printNum(IntPredicate predicate){
		int[] arr = {1,2,3,4,5,6,7,8,9,10};
		for (int i : arr) {
			if(predicate.test(i)){
				System.out.println(i);
			}
		}
	}
}
